package jsu.weizixuan.likesystem.config;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Title: RabbitMQConfig
 * @Author 魏子轩
 * @Package jsu.weizixuan.likesystem.config
 * @Date 2025/9/27 12:06
 * @description: RabbitMQ的配置类
 */
@Configuration
public class RabbitMQConfig {

    @Bean("batchContainerFactory")
    public SimpleRabbitListenerContainerFactory batchFactory(ConnectionFactory cf) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(cf);
        // 重点：开启 List 型参数
        factory.setBatchListener(true);
        factory.setConsumerBatchEnabled(true);
        // 十秒内没有数据再入队列，也执行
        factory.setReceiveTimeout(1000L * 1);
        // 批量接收20条数据
        factory.setBatchSize(20);
        // 手动确认
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        return factory;
    }
}
